<template>
  <div>
    <!--加载数据或提交订单等操作时可以调用，视具体情况而定，添加loding的用意是为了让用户体验更好-->
    <!-- <Spin :spinning="loading" tip="Loading...">-->
    <!-- padding-top 属性设置元素的上内边距（空间） -->
    <div style="padding-top: 1rem;"></div>
    <!-- 整车保修期 -->
    <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 1rem;"></div>
    <!--
      <div
        :style="{
        position: 'absolute',//absolute绝对定位，元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。
        left: 0,
        right: 0,
        zIndex: 9,//position定位如果有重叠的时候，z-index愈大，就显示在最上面 如果两个元素是父节点和子节点的关系，那么子节点显示在父节点的上面，与z-index无关
        borderTop: '1px solid #e9e9e9',//上边框，大小1PX实线颜色#ED9F9F
        padding: '10px 20px',//上下为5px，左右为10px .
        background: '#fff',//白色
        textAlign: 'right'//文本对齐方式
      }"
      > 
      -->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <!--  </Spin>-->
  </div>
</template>
<script>
import { newSave } from '@/api/claim/factory/FullCarWarrantyMaintain';
import { dictionary } from '@/utils/dictMixin';
import { getModelsdictt } from '@/api/dictionary/index';
import Vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
export default {
  mixins: [dictionary],
  components: {
    Vehicle
  },
  data() {
    return {
      loading: false,
      dataList1: [],
      visibleDrawer: false,
      visible: false,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      saveData: null,
      activeName: '审核记录',
      modelNameList: []
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi(){
      const res = await getModelsdictt();
      if (res.resultCode == 200) {
        res.data.forEach(x => {
              if(x != null){
                this.modelNameList.push({text: x.MODEL_NAME,value: x.MODEL_CODE,id: x.MODEL_ID});
              }
            });
        this.formList1.find(x => x.fieldName == 'modelCode').itemList = this.modelNameList;
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        this.modelNameList.forEach(x => {
          if(x.value === this.formList1.find(x => x.fieldName == 'modelCode').initialValue){
            this.saveData.modelId = x.id;
            this.saveData.modelCode = x.value;
            this.saveData.modelName = x.text;
          }
        });
        let res = await newSave(this.saveData);
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    createFormList1() {
      return [
        {
          type: 'SELECT',
          label: '适用车型',
          fieldName: 'modelCode',
          placeholder: '请选择适用车型...',
          initialValue: '',
          readonly: true,
          rules: [{ required: true, message: '请选择适用车型...', trigger: 'change' }],
          itemList: []
        },
        {
          type: 'SELECT',
          label: '营运类型',
          fieldName: 'vpType',
          filterable: true,
          placeholder: '请选择营运类型...',
          initialValue: '',
          rules: [{ required: true, message: '请选择营运类型...', trigger: 'change' }],
          itemList: [
            { text: '运营', value: '55841001' },
            { text: '非运营', value: '55841002' }
          ]
        },
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'vpPeriod',
          placeholder: '请输入保修期...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修期...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change']
            }
          ],
          unitRender: () => {
            return <span>(月)</span>;
          },
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'vpMile',
          placeholder: '请输入保修里程...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修里程...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change']
            }
          ],
          unitRender: () => {
            return <span>(KM)</span>;
          },
          offsetRightCols: 1
        },
        {
          type: 'SELECT',
          label: '状态',
          filterable: true,
          fieldName: 'vpStatus',
          placeholder: '请选择状态...',
          initialValue: 1,
          itemList: [
            { text: '启用', value: 1 },
            { text: '禁用', value: 0 }
          ],
          rules: [{ required: true, message: '请选择状态...', trigger: 'change' }]
        }
      ];
    }
  }
};
</script>
<style lang="less" scoped></style>
